package com.example.springboot.system.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.resp.Result;
import com.example.springboot.common.utils.JwtToken;
import com.example.springboot.system.entity.NewsEntity;
import com.example.springboot.system.mapper.NewsMapper;
import com.example.springboot.system.service.NewsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 咨询表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2025-04-17
 */
@RestController
@RequestMapping("/newsEntity")
public class NewsController {


    private final NewsService service;
    private final NewsMapper mapper;

    public NewsController(NewsService service, NewsMapper mapper) {
        this.service = service;
        this.mapper = mapper;
    }

    @JwtToken
    @Operation(summary = "列表")
    @GetMapping("/newsList")
    public Result<?> newsList() {
        return Result.success(service.list());
    }

    @JwtToken
    @Operation(summary = "获取")
    @PostMapping("/newsId")
    public Result<?> newsId(@RequestParam Integer id) {
        return Result.success(mapper.selectById(id));
    }

    @JwtToken
    @Operation(summary = "列表分页")
    @PostMapping("/newsListPage")
    public Result<?> newsListPage(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<NewsEntity> rowPage = new Page<>(page, pageSize);
        QueryWrapper<NewsEntity> wrapper = new QueryWrapper<>();
        rowPage = mapper.selectPage(rowPage, wrapper);
        return Result.success(rowPage);
    }

    @Data
    static class News {
        private String title;
        private String content;
        private String author;
        private String type;
        private String tag;
        private String source;
        private Integer adminId;
        private String  avatar;
    }

    @JwtToken
    @Operation(summary = "新增")
    @PostMapping("/newsAppend")
    public Result<?> newsAppend(@RequestBody NewsController.News news) {
        NewsEntity entity = new NewsEntity();
        BeanUtils.copyProperties(news, entity);
        return Result.success(service.save(entity));
    }

    @JwtToken
    @Operation(summary = "删除")
    @PostMapping("/newsDelete")
    public Result<?> newsDelete(@RequestParam Integer id) {
        return Result.success(service.removeById(id));
    }

    @JwtToken
    @Operation(summary = "编辑")
    @PostMapping("/newsEditor")
    public Result<?> newsEditor(@RequestBody NewsEntity newsEntity) {
        QueryWrapper<NewsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", newsEntity.getId());
        return Result.success(service.updateById(newsEntity));
    }

    @JwtToken
    @Operation(summary = "搜索")
    @PostMapping("/newsSearch")
    public Result<?> newsSearch(@RequestParam String name) {
        QueryWrapper<NewsEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        return Result.success(mapper.selectList(wrapper));
    }


}
